//
//  main.c
//  C 面向指针编程
//
//  Created by Robin on 16/4/4.
//  Copyright © 2016年 cnbm. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

// 模拟一个链条

//链条的一节
struct node {
    
    struct node *pre;
    struct node *next;
    void *shape;
};

//struct point {
//    double x;
//    double y;
//};
//
//struct rectAngle{
//    double width;
//    double height;
//};

struct pair {
    void *first;
    void *second;
};

struct pair* create_pair (void* a, void* b){
    
    struct pair *p = malloc(sizeof(struct pair));
    p->first = a;
    p->second = b;
    return p;
}

//struct rectAngle* create_rect (double w,double h){
//    
//    struct rectAngle *rect = malloc(sizeof(struct rectAngle));
//    rect->height = h;
//    rect->width = w;
//    return rect;
//}
//
//struct point* create_point (double x,double y){
//    struct point *p = malloc(sizeof(struct point));
//    p->x = x;
//    p->y = y;
//    return p;
//}


struct circle {
    struct point *center;
    double radius;
};

//struct chain_node_shape {
//    struct rectAngle *body;
//    struct circle *holes[2];
//};

//struct chain_node_shape * create_chain_shape (struct circle *c1,struct circle *c2,struct pair *body){
//    
//    struct chain_node_shape *ret = malloc(sizeof(struct chain_node_shape));
//    ret->body = body;
//    ret->holes[0] = c1;
//    ret->holes[1] = c2;
//    return ret;
//}


//struct circle* create_circle (struct pair *p,double radius){
//    
//    struct circle *c1 = malloc(sizeof(struct circle));
//    c1->center = p;
//    c1->radius = radius;
//    
//    return c1;
//}

struct pair* pair_for_double_type (double x,double y);
double* malloc_double (double value);



struct node *create_chain_node(){
    struct node *node = malloc(sizeof(struct node));
    
    // 左边的孔
//    double *left_x = malloc(sizeof(double));
//    double *left_y = malloc(sizeof(double));
//    struct pair *left_center = malloc(sizeof(struct pair));
//
//    *left_y = 1.0;
//    *left_x = 1.0;
//    left_center->first = left_x;
//    left_center->second = left_y;
//    
//    double *left_radius = malloc(sizeof(double));
//    *left_radius = 0.5;
//    
//    struct pair *left_hole = malloc(sizeof(struct pair));
//    left_hole->first = left_center;
//    left_hole->second = left_radius;

    struct pair *left_center = pair_for_double_type(1.0, 1.0);
    //double *left_radius = malloc(sizeof(double));
    double *left_radius = malloc_double(0.5);
    //*left_radius = 0.5;
    struct pair *left_hole = malloc(sizeof(struct pair));
    left_hole->first = left_center;
    left_hole->second = left_radius;
    
    struct pair *right_center = pair_for_double_type(9.0, 1.0);
//    double *right_radius = malloc(sizeof(double));
//    *right_radius = 0.5;
    double *right_radius = malloc_double(0.5);
    
    struct pair *right_hole = malloc(sizeof(struct pair));
    right_hole->first = right_center;
    right_hole->second = right_radius;
    
    struct pair *holes = malloc(sizeof(struct pair));
    holes->first = left_hole;
    holes->second = right_hole;
    
    struct pair *body = pair_for_double_type(10.0, 2.0);
    
    struct pair *shape = malloc(sizeof(struct pair));
    shape->first = body;
    shape->second = holes;
    
    node->next = NULL;
    node->pre = NULL;
    node->shape = shape;
    // 右边的孔
//    double *right_x = malloc(sizeof(double));
//    double *right_y = malloc(sizeof(double));
//    *right_x = 9.0;
//    *right_y = 1.0;
//    
//    struct pair *right_center = malloc(sizeof(struct pair));
//    right_center->first = right_x;
//    right_center->second = right_y;
//    
//    double *right_radius = malloc(sizeof(double));
//    *right_radius = 0.5;
//    
//    struct pair *right_hole = malloc(sizeof(struct pair));
//    right_hole->first = right_center;
//    right_hole->second = right_radius;
    
//    // 长宽
//    double *width = malloc(sizeof(double));
//    double *height = malloc(sizeof(double));
//    *width = 10.0;
//    *height = 2.0;
//    
//    struct pair *body = malloc(sizeof(struct pair));
//    body->first = width;
//    body->second = height;
//    
//    struct pair *holes = malloc(sizeof(struct pair));
//    holes->first = left_hole;
//    holes->second = right_hole;
//    
//    struct pair *shape = malloc(sizeof(struct pair));
//    shape->first = body;
//    shape->second = holes;
//    
//    node->shape = shape;
//    node->pre = NULL;
//    node->next = NULL;
    
    return node;
}


//struct node* create_chain_node (void){
//    
//    double radius = 0.5;
//    double left_x = 1.0;
//    double left_y = 1.0;
//    struct point *left_center = create_point(left_x, left_y);
//    struct circle *c1 = create_circle(left_center, radius);
//    
//    double right_x = 9.0;
//    double right_y = 1.0;
//    struct point *right_center = create_point(right_x, right_y);
//    struct circle *c2 = create_circle(right_center, radius);
//    
//    struct rectAngle *rect = create_rect(10.0, 2.0);
//    
//    struct chain_node_shape *shape = create_chain_shape(c1, c2, rect);
//    
//    struct node *node = malloc(sizeof(struct node));
//    node->next = NULL;
//    node->pre = NULL;
//    node->shape = shape;
//    
//    return node;
//}

int main(int argc, const char * argv[]) {

    //
//    struct node a,b,c;
//    
//    a.next = &b;
//    b.pre = &a;
//    
//    b.next = &c;
//    c.pre = &b;
//    
//    c.next = &a;
//    a.pre = &c;
//    
//    // 建造一个1000节的链条
//    struct node *head = malloc(sizeof(struct node));
//    struct node *tail = head;
//    
//    for (int i=0; i<1000; i++) {
//        
//        struct node *new_tail = malloc(sizeof(struct node));
//        tail->next = new_tail;
//        new_tail->pre = tail;
//        
//        tail = new_tail;
//    }
//    
//    tail->next = head;
//    head->pre = tail;
    
    struct node *head = create_chain_node();
    struct node *tail = head;
    
    for (int i=0; i<1000; i++) {
        
        struct node *new_node = create_chain_node();

        tail->next = new_node;
        new_node->pre = tail;
        
        tail = new_node;
    }
    
    tail->next = head;
    head->pre = tail;
    
    
    return 0;
}

double* malloc_double (double value){
    double *d = malloc(sizeof(double));
    *d = value;
    return d;
}

struct pair* pair_for_double_type (double x,double y){
    
    struct pair *ret = malloc(sizeof(struct pair));
    ret->first = malloc_double(x);
    ret->second = malloc_double(y);
    //    double *first = malloc(sizeof(double));
    //    double *second = malloc(sizeof(double));
    //    *first = x;
    //    *second = y;
    //    ret->first = first;
    //    ret->second = second;
    return ret;
}


